package com.aloda.service.impl;

import com.aloda.constant.EmpConstant;
import com.aloda.constant.ExceptionConstant;
import com.aloda.constant.JwtClaimsConstant;
import com.aloda.constant.RedisConstant;
import com.aloda.dto.EmpLoginDTO;
import com.aloda.entity.Emp;
import com.aloda.exception.BaseException;
import com.aloda.exception.VerifyException;
import com.aloda.mapper.EmpMapper;
import com.aloda.properties.JwtProperties;
import com.aloda.properties.SaltProperties;
import com.aloda.service.EmpService;
import com.aloda.utils.AccountUtils;
import com.aloda.utils.JwtUtil;
import com.aloda.utils.PasswordUtils;
import com.aloda.vo.EmpLoginVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author aloda
 * @description 针对表【emp(管理员表)】的数据库操作Service实现
 * @createDate 2024-10-21 20:27:21
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Resource
    private EmpMapper empMapper;

    @Resource
    private JwtProperties jwtProperties;

    @Resource
    private SaltProperties saltProperties;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 员工后台登录
     *
     * @param empLoginDTO
     * @return
     */
    @Override
    public EmpLoginVO empLogin(EmpLoginDTO empLoginDTO) {
        // 接收账号密码
        String empAccount = empLoginDTO.getEmpAccount();
        String empPassword = empLoginDTO.getEmpPassword();
        // 校验账号密码格式
        if (!AccountUtils.checkAccount(empAccount)) {
            throw new BaseException(ExceptionConstant.ACCOUNT_ERROR);
        }
        if (!PasswordUtils.checkPassword(empPassword)) {
            throw new BaseException(ExceptionConstant.PASSWORD_ERROR);
        }
        // 校验验证码
        VerifyServiceImpl.checkVerify(empLoginDTO.getCode(), empLoginDTO.getKey(), stringRedisTemplate);
        // 查询账号密码是否正确
        LambdaQueryWrapper<Emp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String transPassword = DigestUtils.md5DigestAsHex((empPassword + saltProperties.getPasswordSalt()).getBytes());
        lambdaQueryWrapper.eq(Emp::getEmpAccount, empAccount).eq(Emp::getEmpPassword, transPassword);
        Emp emp = empMapper.selectOne(lambdaQueryWrapper);
        // 不正确返回错误信息
        if (emp == null) {
            throw new BaseException(ExceptionConstant.LOGIN_FAIL);
        }
        // 校验账号状态
        if (emp.getEmpStatus() == EmpConstant.ACCOUNT_BANNED) {
            throw new BaseException(ExceptionConstant.ACCOUNT_BANNED_CODE, ExceptionConstant.ACCOUNT_BANNED);
        }
        // 登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, emp.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        // 返回用户登录VO
        return EmpLoginVO.builder()
                .id(emp.getId())
                .username(emp.getEmpAccount())
                .token(token)
                .build();
    }
}




